package com.luhuiguo.cms.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.AppResources;
import com.luhuiguo.cms.client.presenter.ChannelsPresenter;

@Singleton
public class ChannelsView extends Composite implements ChannelsPresenter.Display {

    private static ChannelsViewUiBinder uiBinder = GWT.create(ChannelsViewUiBinder.class);
    
    interface ChannelsViewUiBinder extends UiBinder<Widget, ChannelsView> {
    }

    private AppResources resources;

    @UiField
    Button refreshButton;

    @UiField
    Button addButton;

    @UiField
    Button editButton;

    @UiField
    Button deleteButton;

    @UiField(provided = true)
    Tree channelTree;

    @Inject
    public ChannelsView(AppResources appResources) {

        this.resources = appResources;

        channelTree = new Tree(resources);
        // addButton.setEnabled(enabled)

        initWidget(uiBinder.createAndBindUi(this));

    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public Tree getTree() {
        return channelTree;
    }

    @Override
    public Button getAddClick() {
        return addButton;
    }

    @Override
    public Button getDeleteClick() {
        return deleteButton;
    }

    @Override
    public Button getEditClick() {
        return editButton;
    }

    @Override
    public Button getRefreshClick() {

        return refreshButton;
    }

    @Override
    public void bindTreeItems(List<TreeItem> treeList) {
        channelTree.clear();

        for (int i = 0; i < treeList.size(); i++) {
            TreeItem item = treeList.get(i);

            channelTree.addItem(item);

            if (channelTree.getItemCount() > 0) {
                TreeItem root = channelTree.getItem(0);
                root.setState(true);
                channelTree.setSelectedItem(root);
            }
        }
    }

    @Override
    public void setLoading(boolean loading) {

    }

    @Override
    public void deleteSelectedChannel() {
        channelTree.getSelectedItem().remove();
        if (channelTree.getItemCount() > 0) {
            TreeItem root = channelTree.getItem(0);
            // root.setState(true);
            channelTree.setSelectedItem(root);
        }

    }

    @Override
    public void setAddEnabled(boolean enabled) {
        addButton.setEnabled(enabled);

    }

    @Override
    public void setDeleteEnabled(boolean enabled) {
        deleteButton.setEnabled(enabled);

    }

    @Override
    public void setEditEnabled(boolean enabled) {
        editButton.setEnabled(enabled);

    }

}
